﻿function IsNullOrEmpty(s) {
    return (s == null || s == "" || s == undefined);
}
function JSONFormat(json, hasarr) {
    if (hasarr) {
        for (var c in json) {
            if (json[c] instanceof Array) {
                json[c] = json[c].toString();
            }
        };
    }
    return JSON.stringify(json);
}
/*Start*********************************************************
 * jquery.min.js
 */
function ValueReset(array) {
    array.forEach(function (selecter) {
        $(selecter).val("");
    })
}
function GetFormData(selecter) {
    var d = {};
    var t = $(selecter).serializeArray();
    $.each(t, function () {
        d[this.name] = this.value;
    });
    return d;
}
function GetFormDataString(selecter) {
    return $(selecter).serialize();
}
function SetFormData(selecter, data) {
    var obj = $(selecter);
    $.each(data, function (name, ival) {
        var $input = obj.find("[name='" + name + "']");
        if ($input.length == 0) { return;}
        if ($input.attr("type") == "radio" || $input.attr("type") == "checkbox") {
            $input.each(function () {
                if (Object.prototype.toString.apply(ival) == '[object Array]') {
                    for (var i = 0; i < ival.length; i++) {
                        if ($(this).val() == ival[i])
                            $(this).attr("checked", "checked");
                    }
                } else {
                    if ($(this).val() == ival)
                        $(this).attr("checked", "checked");
                }
            });
        } else if ($input.attr("type") == "textarea" || $input.prop("tagName") == "textarea") {
            obj.find("[name=" + name + "]").html(ival);
        } else {
            obj.find("[name=" + name + "]").val(ival);
        }
    })
}
/*Start*********************************************************
 * bootstrap-table.min.js
 */
function TableRefresh(selecter) {
    $(selecter).bootstrapTable("refresh");
}
function GetTableSelections(selecter) {
    return $(selecter).bootstrapTable("getSelections");
}
/*Start*********************************************************
 * toastr.min.js
 */
function MsgClear() {
    toastr.clear();
}
function ShowMsg(msg, type) {
    //alert(msg);
    if (IsNullOrEmpty(type)) { type = "info"; }
    toastr[type](msg);
}
/*Start*********************************************************
 * moment-with-localse.js 
 * hooks.locale('en');-->hooks.locale('zh-cn');
 */
function GetDateSubtractFromNow(d1) {
    //let start_date = moment().subtract(1, 'weeks');
    //let end_date = moment();

    //end_date.diff(start_date); // 返回毫秒数

    //end_date.diff(start_date, 'months'); // 0
    //end_date.diff(start_date, 'weeks'); // 1
    //end_date.diff(start_date, 'days'); // 7
    //start_date.diff(end_date, 'days'); // -7

    var start_date = moment().endOf('day');
    var end_date = moment(d1);
    var temp = start_date.diff(end_date, 'hours');
    return temp;
}
function GetDateSubtractFromNow2(d1) {
    var start_date = moment().endOf('day');
    var end_date = moment(d1);
    var temp = start_date.diff(end_date, 'days');
    return temp;
}
function GetDateSubtract(d1, d2) {
    var start_date = moment(d1);
    var end_date = moment(d2);
    var temp = end_date.diff(start_date, 'days');
    return temp;
}
function DateTimeFromNow(d1) {
    return moment(d1).fromNow();
}
/*Start*********************************************************
 * moment-with-localse.js 
 */
function ResetForm(selecter, valclear) {
    $(selecter).data('bootstrapValidator').resetForm(valclear);
}
function FormIsValid(selecter) {
    var bootstrapValidator = $(selecter).data('bootstrapValidator');
    bootstrapValidator.validate();
    return bootstrapValidator.isValid();
}